package com.ruoyi.project.gxgw_modules.adsList.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.adsList.domain.AdsList;
import com.ruoyi.project.gxgw_modules.adsList.service.IAdsListService;
import com.ruoyi.project.gxgw_modules.adsense.domain.Adsense;
import com.ruoyi.project.gxgw_modules.adsense.service.IAdsenseService;
import com.ruoyi.project.gxgw_modules.systemConfig.domain.SystemConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;

/**
 * 广告管理 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-17
 */
@Controller
@RequestMapping("/gxgw_modules/adsList")
public class AdsListController extends BaseController {
	private String prefix = "gxgw_modules/adsList";

	@Autowired
	private IAdsListService adsListService;
	
	@Autowired
	private IAdsenseService adsenseService;

	@RequiresPermissions("gxgw_modules:adsList:view")
	@GetMapping()
	public String adsList() {
		return prefix + "/adsList";
	}

	/**
	 * 查询广告管理列表
	 */
	@RequiresPermissions("gxgw_modules:adsList:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(AdsList adsList) {
		startPage();
		if(adsList.getAdsense()!=null&&adsList.getKey()!=null)
		System.out.println(adsList.getAdsense().getAdsName()+" "+adsList.getKey());
		List<AdsList> list = adsListService.selectAdsListList(adsList);
		return getDataTable(list);
	}
	
	@RequiresPermissions("gxgw_modules:adsList:list")
	@PostMapping("/selectAdsName")
	@ResponseBody
	public List<Adsense> selectAdsName() {
		
		List<Adsense> adsense = adsenseService.selectAdsName();
		
		return adsense;
	}

	/**
	 * 导出广告管理列表
	 */
	@RequiresPermissions("gxgw_modules:adsList:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(AdsList adsList) {
		List<AdsList> list = adsListService.selectAdsListList(adsList);
		ExcelUtil<AdsList> util = new ExcelUtil<AdsList>(AdsList.class);
		return util.exportExcel(list, "adsList");
	}

	/**
	 * 新增广告管理
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存广告管理
	 */
	@RequiresPermissions("gxgw_modules:adsList:add")
	@Log(title = "广告管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(AdsList adsList,MultipartFile file) {
		try {
            if (!file.isEmpty()) {
            	  String filename = FileUploadUtils.upload(RuoYiConfig.getProfile() + "adsList/", file);
            	  adsList.setAdsListImage(filename);
            }
           
        } catch (Exception e) {

            return error(e.getMessage());
        }
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		adsList.setCreateTime(timeStamp);
		adsList.setAdsListPublic(timeStamp);
		adsList.setCreateBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(adsListService.insertAdsList(adsList));
	}
	

	/**
	 * 修改广告管理
	 */
	@GetMapping("/edit/{adsListId}")
	public String edit(@PathVariable("adsListId") Integer adsListId, ModelMap mmap) {
		AdsList adsList = adsListService.selectAdsListById(adsListId);
		mmap.put("adsList", adsList);
		return prefix + "/edit";
	}

	/**
	 * 修改保存广告管理
	 * 
	 */
	@RequiresPermissions("gxgw_modules:adsList:edit")
	@Log(title = "广告管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(AdsList adsList,MultipartFile file)  {
		AdsList ad = adsListService.selectAdsListById(adsList.getAdsListId());
		System.out.println(file.getOriginalFilename());
		try {
            if (!file.isEmpty()) {
            	if(ad.getAdsListImage() != file.getOriginalFilename()) {
            	  String filename = FileUploadUtils.upload(RuoYiConfig.getProfile() + "adsList/", file);
            	  adsList.setAdsListImage(filename);
            	}
            }
           
        } catch (Exception e) {

            return error(e.getMessage());
        }
	
		
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		adsList.setUpdateTime(timeStamp);
		//设置修改人
		adsList.setUpdateBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(adsListService.updateAdsList(adsList));
	}

	/**
	 * 删除广告管理
	 */
	@RequiresPermissions("gxgw_modules:adsList:remove")
	@Log(title = "广告管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		
		return toAjax(adsListService.deleteAdsListByIds(ids));
	}

}
